<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>弓友之家</title>
    <link rel="stylesheet" href="css/mui.css" />
    <link rel="stylesheet" href="fonts/iconfont.css" />
    <link rel="stylesheet" href="css/style.css" />
</head>
<body class="bg1">
	<div class="mui-content" style="overflow: hidden;">
		<div class="logo"><img src="images/logo_w.png"/></div>
		<form class="login">
			<div class="item mui-clearfix"> 
				<label><i class="iconfont icon-gerenzhongxin"></i></label>
				<input type="number" id="mobile" placeholder="输入您的手机号" />
			</div> 
			<div class="item mui-clearfix">
				<label><i class="iconfont icon-mima"></i></label>
				<input type="password" id="password" placeholder="输入您的登录密码" />
			</div>
			<div class="mui-clearfix edit-pwd">
				<div class="mui-pull-left" onclick="remPwd(this)">
					<i class="on"></i>记住密码
				</div>
				<div class="mui-pull-right">
					<a href="findpwd.html">忘记密码？</a>
				</div>
			</div>
			<div class="button">
				<button type="button" class="on" onclick="login()">登录</button>
				<button type="button" onclick="register()">注册</button>
			</div>
			<div class="q-login">
				<a href="javascript:void(0)"  id="weixin"><img src="images/icon1.png"/></a>
			</div>
		</form>
	</div>
	<script type="text/javascript" src="js/jquery-1.8.3.min.js" ></script>
	<script type="text/javascript" src="js/mui.min.js" ></script>
	<script type="text/javascript" src="js/common.js" ></script>
	<script type="text/javascript" charset="utf-8">
      	mui.init();
      	function login(){ 
      		var mobile=$("#mobile").val();
      		var password=$("#password").val();
			mui.post(H_SRC + '/amier/user/login', {
				mobile: mobile,
				password: password
			}, function(data) {
				if(data.code == 1) {
					if($(".edit-pwd .mui-pull-left i").hasClass("on")){
						set("token", data.token);
					}
					mui.openWindow({
						url: 'index.html',
						id: 'index.html',
						createNew:true,
						show: {
							autoShow: true,
							aniShow: 'slide-in-right',
							duration: 200
						}
					})
					setTimeout(function() {
						mui.currentWebview.close('none');
					}, 300);
				} else {
					mui.toast(data.msg);
					return false;
				}
			}, 'json');
		}
		
		//进入注册
		function register() {
			mui.openWindow({
				url: 'register.html',
				id: 'register.html',
				createNew:true,
				show: {
					autoShow: true,
					aniShow: 'slide-in-right',
					duration: 200
				}
			})
		}
		
		//记住密码
		function remPwd(obj){
			if($(obj).find("i").hasClass("on")){
				$(obj).find("i").removeClass("on");
			}else{
				$(obj).find("i").addClass("on");
			}
		}
		
		mui.plusReady(function() {  
            plus.oauth.getServices(function(services) {
                auths = services;
            }, function(e) {
                alert("获取登录服务列表失败：" + e.message + " - " + e.code);
            });
        })
		document.getElementById('weixin').addEventListener('tap',function() {
//          mui.toast("微信");
            authLogin('weixin');
//			authLogout();
//			return false;
        })
		
		 // 登录操作
        function authLogin(type) {
            var s;
            for (var i = 0; i < auths.length; i++) {
                if (auths[i].id == type) {
                    s = auths[i];
                    break;
                }
            }
            if (!s.authResult) {
                s.login(function(e) {
                    mui.toast("登录认证成功！");
                    authUserInfo(type); 
                }, function(e) {
                    mui.toast("登录认证失败！");
                });
            } else {
                mui.toast("已经登录认证！");
                authUserInfo(type);
            }
        }
        //注销
        function authLogout() {
            for (var i in auths) {
                var s = auths[i];
                if (s.authResult) {
                    s.logout(function(e) {
                        mui.toast("注销登录认证成功！");
                    }, function(e) {
                        mui.toast("注销登录认证失败！");
                    });
                }
            }
        }
        // 微信登录认证信息
        function authUserInfo(type) {
            var s;
            for (var i = 0; i < auths.length; i++) {
                if (auths[i].id == type) {
                    s = auths[i];
                    break;
                }
            }
            if (!s.authResult) {
                mui.toast("未授权登录！");
            } else {
                s.getUserInfo(function(e) {
                    var josnStr = JSON.stringify(s.userInfo);
                    var jsonObj = s.userInfo;
                    console.log("获取用户信息成功：" + josnStr);
                    showData(type,jsonObj);
//                  authLogout();
                }, function(e) {
                    alert("获取用户信息失败：" + e.message + " - " + e.code);
                });
            }
        }
        // 显示用户头像信息
        function showData(type,data) {
        	var data=JSON.stringify(data);
        	mui.ajax(H_SRC+'/amier/index/wx_login',{
				data:{
					obj:data
				},
				dataType:'json',
				type:'post',
				timeout:10000,             
				success:function(data){
					if(data.code==2){
						var open_id=data.data.open_id;
						mui.openWindow({
							url:'wanshan.html',
							id:'wanshan.html',
							createNew:true,
							show: {
								autoShow: true,
								aniShow: 'slide-in-right',
								duration: 200
							},
							extras:{
								open_id:open_id
							}
						})
					}else{
						set("token", data.token);
						mui.openWindow({
							url: 'index.html',
							id: 'index.html',
							createNew:true,
							show: {
								autoShow: true,
								aniShow: 'slide-in-right',
								duration: 200
							}
						})
						setTimeout(function() {
							mui.currentWebview.close('none');
						}, 300);
					}
				},
				error:function(xhr,type,errorThrown){
					alert(type)
				}
			})
            switch (type){
                case 'weixin':
                    headImage.src = data.headimgurl;
                    break;
                default:
                    break;
            }
        }
	</script>
</body>
</html>